﻿using UnityEngine;
using System.Collections;

public class WaveRender : MonoBehaviour {
	private Mesh mesh;
	[HideInInspector]
	public Vector2[] uvs;
	public Texture2D texture;
	// Use this for initialization
	public void Init () {
		mesh = GetComponent<MeshFilter>().mesh;
		uvs = new Vector2[GetComponent<Waves> ().vertCount*2];
		GetComponent<MeshRenderer> ().sharedMaterial = new Material (GetComponent<MeshRenderer> ().sharedMaterial);
		GetComponent<MeshRenderer> ().sharedMaterial.mainTexture = texture;
	}

	public void GenerateRender(){
		Waves wave = GetComponent<Waves> ();
		for (var i = 0; i < wave.vertCount; i++) {
			float oX = (float)i / (float)wave.vertCount;
			float oY = (wave.nowVertex [i].y + (wave.height / 2f)) / wave.height;
			uvs [i] = new Vector2 (oX, oY);
			uvs [i + wave.vertCount] = new Vector2 (oX, 0f);
		}
		mesh.uv = uvs;
	}

	// Update is called once per frame
	void Update () {	
		
	}
}
